package com.panshi.controller;

import com.google.common.collect.Maps;
import com.panshi.api.SysUserApi;
import com.panshi.utils.JwtUtil;
import com.panshi.vo.CodeMsgConstant;
import com.panshi.vo.R;
import com.panshi.vo.SysUserVO;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

@Controller
public class LoginController {


    @Autowired
    private SysUserApi sysUserApi;

    /**
     * 去登录页面
     * @return
     */
    @GetMapping("/login")
    public String toLogin(){
        return "login";
    }


    /**
     * 登录请求
     * @param sysUser
     * @return
     */
    @PostMapping("/login")
    @ResponseBody
    @ApiOperation("用户登录")
    public R login(@RequestBody @Validated @ApiParam(value = "SysUserVO" , name = "用户登录参数") SysUserVO sysUser, HttpSession session){

        if (StringUtils.isEmpty(sysUser.getUsername())) {
            return R.error().msg("请输入用户名");
        }

        if(StringUtils.isEmpty(sysUser.getPassword())){
            return R.error().msg("请输入密码");
        }

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(sysUser.getUsername(),sysUser.getPassword());

        try{
            subject.login(token);
            SysUserVO userDB = sysUserApi.getByUsername(sysUser.getUsername());

            Map<String, Object> claims = Maps.newHashMap();
            claims.put("username",sysUser.getUsername());
            claims.put("userId",userDB.getId());

            String jwtToken = JwtUtil.createJWT(new Date(3600L * 72), claims);

            session.setAttribute("userInfo",userDB);

            return R.ok().msg("登录成功2").data("jwtToken",jwtToken);
        }catch(UnknownAccountException e){
            e.printStackTrace();
            return R.error().msg("账号不存在3");
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            return R.error().msg("密码错误4");
        }catch (Exception e){
            e.printStackTrace();
            return R.error().msg("登录异常,请稍后再试5");
        }

    }

    @GetMapping("/noauth")
    public String noauth(){
        return "noauth";
    }


    @GetMapping("/loginout")
    @ResponseBody
    public R logout(HttpSession session){
        session.removeAttribute("userInfo");
        System.out.println(session.getAttribute("userInfo"));
        return R.ok(CodeMsgConstant.CODE_0);
    }
}
